package com.zcx.merchantorder.view;

import android.app.Activity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @Description: findViewById封装
 * @Author: zcx
 * @CreateDate: 2019/3/3 17:58
 */
public class FindView {

    private static Activity activity;

    // 运用了单例模式中的饿汉式
    private static final FindView findView = new FindView();

    /**
     * 获取Activity实例
     *
     * @param activity
     */
    private static void setActivity(Activity activity) {
        FindView.activity = activity;
    }

    /**
     * 初始化FindView
     *
     * @param activitys
     * @return
     */
    public static FindView with(Activity activitys) {
        setActivity(activitys);
        return findView;
    }

    /**
     * 根据Id获取View实例
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int id) {
        View view = activity.findViewById(id);
        return (T) view;
    }

    public CompoundButton compoundButton(final int id) {
        return getView(id);
    }

    /**
     * 设置TextView内容
     *
     * @param id
     * @param textId
     * @return
     */
    public FindView setText(int id, int textId) {
        TextView textView = getView(id);
        textView.setText(textId);
        return this;
    }

    /**
     * 设置ImageView 资源
     *
     * @param id
     * @param imgResource
     * @return
     */
    public FindView setImageResource(int id, int imgResource) {
        ImageView iv = getView(id);
        iv.setImageResource(imgResource);
        return this;
    }

    /**
    *按钮点击事件
     * @date: 2019/3/3 19:00
     * @author: zcx
     * @param id
     * @param listener
     * @return android.view.View
     */
    public View onClick(final int id, final View.OnClickListener listener) {
        View clickable = getView(id);
        clickable.setOnClickListener(listener);
        return clickable;
    }

    /**
    *按钮点击事件实现接口
     * @date: 2019/3/3 19:01
     * @author: zcx
     * @param id
     * @param runnable
     * @return android.view.View
     */
    public View onClick(final int id, final Runnable runnable) {
        return onClick(id, new View.OnClickListener() {

            public void onClick(View v) {
                runnable.run();
            }
        });
    }

    /**
    *将所有的id传入实现点击事件
     * @date: 2019/3/3 19:01
     * @author: zcx
     * @param listener
     * @param ids
     * @return void
     */
    public void onClick(final View.OnClickListener listener, final int... ids) {
        for (int id : ids)
            getView(id).setOnClickListener(listener);
    }

    /**
    *将所有的id传入实现点击事件接口
     * @date: 2019/3/3 19:01
     * @author: zcx
     * @param runnable
     * @param ids
     * @return void
     */
    public void onClick(final Runnable runnable, final int... ids) {
        onClick(new View.OnClickListener() {

            public void onClick(View v) {
                runnable.run();
            }
        }, ids);
    }

    /**
    *根据资源设置图片
     * @date: 2019/3/3 19:02
     * @author: zcx
     * @param id
     * @param drawable
     * @return android.widget.ImageView
     */
    public ImageView setDrawable(final int id, final int drawable) {
        ImageView image = getView(id);
        image.setImageDrawable(image.getResources().getDrawable(drawable));
        return image;
    }

    /**
    *事件变化监听
     * @date: 2019/3/3 19:02
     * @author: zcx
     * @param id
     * @param listener
     * @return android.widget.CompoundButton
     */
    public CompoundButton onCheck(final int id, final CompoundButton.OnCheckedChangeListener listener) {
        CompoundButton checkable = getView(id);
        checkable.setOnCheckedChangeListener(listener);
        return checkable;
    }

    /**
    *事件变化监听事件接口
     * @date: 2019/3/3 19:03
     * @author: zcx
     * @param id
     * @param runnable
     * @return android.widget.CompoundButton
     */
    public CompoundButton onCheck(final int id, final Runnable runnable) {
        return onCheck(id, new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                runnable.run();
            }
        });
    }

    /**
    *所有id的变化事件监听
     * @date: 2019/3/3 19:03
     * @author: zcx
     * @param listener
     * @param ids
     * @return void
     */
    public void onCheck(final CompoundButton.OnCheckedChangeListener listener, final int... ids) {
        for (int id : ids)
            compoundButton(id).setOnCheckedChangeListener(listener);
    }

    /**
    *所有id的变化事件接口实现
     * @date: 2019/3/3 19:03
     * @author: zcx
     * @param runnable
     * @param ids
     * @return void
     */
    public void onCheck(final Runnable runnable, final int... ids) {
        onCheck(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                runnable.run();
            }
        }, ids);
    }
}
